﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.CommonModels;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Ekton_Manage_Business
{
    public class CommonApi : ICommonApi
    {
        private readonly IBaseRepository<product_type, int> _baseproductTypeRepository;
        private readonly IBaseRepository<company, int> _basecompanyRepository;
        private readonly IBaseRepository<series_msg, int> _baseseriesMsgRepository;
        private readonly IBaseRepository<brand_msg, int> _basebrandMsgRepository;
        private readonly IBaseRepository<material, int> _basematerialRepository;
        private readonly IBaseRepository<sys_role, int> _basesysRoleRepository;
        private readonly IBaseRepository<platform_msg, int> _baseplatformMsgRepository;

        public CommonApi(IBaseRepository<product_type, int> baseproductTypeRepository, IBaseRepository<company, int> basecompanyRepository, IBaseRepository<series_msg, int> baseseriesMsgRepository, IBaseRepository<brand_msg, int> basebrandMsgRepository, IBaseRepository<material, int> basematerialRepository, IBaseRepository<sys_role, int> basesysRoleRepository, IBaseRepository<platform_msg, int> baseplatformMsgRepository)
        {
            _baseproductTypeRepository = baseproductTypeRepository;
            _basecompanyRepository = basecompanyRepository;
            _baseseriesMsgRepository = baseseriesMsgRepository;
            _basebrandMsgRepository = basebrandMsgRepository;
            _basematerialRepository = basematerialRepository;
            _basesysRoleRepository = basesysRoleRepository;
            _baseplatformMsgRepository = baseplatformMsgRepository;
        }

        /// <summary>
        /// 查询所属公司组织
        /// </summary>
        /// 
        public async Task<Message> GetCompany()
        {
            var data = await _basecompanyRepository.QueryTableList(m => true);
            List<DtoCompany> result = new();
            data.ForEach((m) =>
            {
                result.Add(new DtoCompany
                {
                    id = m.id,
                    company_name = m.company_name
                });
            });
            return new Message(result);
        }

        /// <summary>
        /// 查询品牌
        /// </summary>
        /// 
        public async Task<Message> GetBrand()
        {
            var data = await _basebrandMsgRepository.QueryTableList(m => true);
            List<DtoBrand> result = new();
            data.ForEach((m) =>
            {
                result.Add(new DtoBrand
                {
                    id = (int)m.id,
                    product_brand = m.product_brand
                });
            });
            return new Message(result);
        }
        /// <summary>
        /// 查询产品材料
        /// </summary>
        /// 
        public async Task<Message> GetMaterial()
        {
            var data = await _basematerialRepository.QueryTableList(m => true);
            List<DtoMaterial> result = new();
            data.ForEach((m) =>
            {
                result.Add(new DtoMaterial
                {
                    id = m.id,
                    materials_name = m.materials_name
                });
            });
            return new Message(result);
        }
        /// <summary>
        /// 查询系列信息
        /// </summary>
        /// 
        public async Task<Message> GetSeriesMsg(int brandId)
        {
            //根据品牌id来查询系列信息，如果brandId为0，则默认查询全部系列信息
            if(brandId == 0)
            {
                var data = await _baseseriesMsgRepository.QueryTableList(m => true);
                List<DtoSeries> result = new();
                data.ForEach((m) =>
                {
                    result.Add(new DtoSeries
                    {
                        id = m.id,
                        seriesname = m.seriesname
                    });
                });
                return new Message(result);
            }
            else
            {
                var data = await _baseseriesMsgRepository.QueryTableList(m => m.brand_id == brandId);
                if (data == null) return new Message(false, 202);
                List<DtoSeries> result = new();
                data.ForEach((m) =>
                {
                    result.Add(new DtoSeries
                    {
                        id = m.id,
                        seriesname = m.seriesname
                    });
                });
                return new Message(result);
            }
        }

        /// <summary>
        /// 查询全部平台
        /// </summary>
        /// 
        public async Task<Message> GetPlatform(int series_id)
        {
            if (series_id == 0)//如果等于0则查询全部平台
            {
                var result = await _baseplatformMsgRepository.QueryTableList(m => true);
                return new Message(result);
            }
            else//如果不等于0则查询相关联的平台
            {
                var result = await _baseplatformMsgRepository.QueryTableList(m => m.series_id == series_id);
                return new Message(result);
            }
        }

        /// <summary>
        /// 查询产品分类
        /// </summary>
        /// 
        public async Task<Message> GetProductType()
        {
            var data = await _baseproductTypeRepository.QueryTableList(m => true);
            List<DtoProduct> result = new();
            data.ForEach((m) =>
            {
                result.Add(new DtoProduct
                {
                    id = m.id,
                    father_id = m.father_id,
                    father_name = m.father_name,
                    class_name = m.class_name
                });
            });
            return new Message(result);
        }

        /// <summary>
        /// 查询全部角色
        /// </summary>
        /// 
        public async Task<Message> GetRoleInfo()
        {
            var data = await _basesysRoleRepository.QueryTableList(m => true);
            List<DtoRole> result = new();
            data.ForEach((m) =>
            {
                result.Add(new DtoRole
                {
                    id = m.id,
                    rolename = m.rolename
                });
            });
            return new Message(result);
        }

        /// <summary>
        /// 根据材料的id来查询对应的颜色
        /// </summary>
        /// 
        public async Task<Message> GetColorByMaterial(int materialId)
        {
            var result = await _basematerialRepository.QuerySingleData(materialId);
            return new Message(result.color);
        }
    }
}
